package com.sdry.web.controller.lz;

import com.sdry.model.llm.User;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @ClassName LoginFilter
 * @Description 过滤器
 * @Author lz
 * @Date 2019年1月2日 14:55:03
 * @Version 1.0
 */
public class LoginFilter implements Filter {
    //Filter
    public FilterConfig config;
    //放行路径
    public List<String> doFilterURL;

    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletrequest;
        HttpServletResponse response = (HttpServletResponse) servletresponse;
        String servletPath = request.getServletPath();
        //System.out.println("servletPath=" + servletPath);
        User user = (User)request.getSession().getAttribute("currentUser");
        //System.out.println("user=" + user);
        //只要用户回到登录页面，就把session销毁，这样就需要重新登录
        if(servletPath.contains("/login.do") || servletPath.contains("")){
            System.out.println("销毁session");
            request.getSession().removeAttribute("currentUser");
        }
        //如果用户没有登录是不能直接进入其他页面的
        if(user != null){
            System.out.println("===放行===");
            chain.doFilter(request, response);
            return;
        } else {
            System.out.println("===进入判断体===");
            request.getRequestDispatcher("/login.do").forward(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
